package meijuli.com.join51.business.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.adapter.WithdrawWayAdapter;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import com.jhx.common.model.EventBean;
import meijuli.com.join51.business.model.WithdrawRule;
import com.jhx.common.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import meijuli.com.join51.business.service.ApiService;
import retrofit2.Call;

/**
 * 兑换方式
 *
 * @author Wizong 2017-05-19 13:09
 */
public class WithdrawWayFragment extends BaseFragment {
    private static final String KEY_WITHDRAW_WAY = "key_withdraw_way";
    private static final String KEY_WITHDRAW_TYPE = "key_withdraw_type";
    RecyclerView mRecycleWithdrawWay;
    TextView mTextRate;
    Button extractBtn;

    private ApiManager manager = ApiManager.get();
    private String withdrawType;
    private WithdrawWayAdapter mAdapter;

    private List<WithdrawRule> mRuleList = new ArrayList<>();
    private WithdrawRule mCurrentRule;


    public static Fragment newInstance(WithdrawRule withdrawRule, String withdrawType) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_WITHDRAW_WAY, withdrawRule);
        bundle.putString(KEY_WITHDRAW_TYPE, withdrawType);
        WithdrawWayFragment fragment = new WithdrawWayFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_withdraw_way, container, false);
        mRecycleWithdrawWay = view.findViewById(R.id.recycle_withdraw_way);
        mTextRate = view.findViewById(R.id.text_withdraw_rate);
        extractBtn = view.findViewById(R.id.extract_btn);
        extractBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onViewClicked();
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        WithdrawRule withdrawRule = (WithdrawRule) getArguments().getSerializable(KEY_WITHDRAW_WAY);
        String withdrawType = getArguments().getString(KEY_WITHDRAW_TYPE);
        if (withdrawRule != null) {
            mCurrentRule = withdrawRule;
        }
        if (withdrawType != null) {
            this.withdrawType = withdrawType;
        }

        mRecycleWithdrawWay.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new WithdrawWayAdapter(getActivity(), mRuleList);
        mRecycleWithdrawWay.setAdapter(mAdapter);
    }

    private void initData() {
        showLoadingDialog();
        Call<HttpResponse<List<WithdrawRule>>> withdrawRuleList = manager.getApiService(true, ApiService.class)
                .getWithdrawRuleList();
        HttpRequest.getRequest().autoRecycle(this).start(withdrawRuleList, new ResponseCallback<List<WithdrawRule>>() {
            @Override
            public void onSuccess(String url, List<WithdrawRule> data) {
                dismissDialog();
                List<WithdrawRule> ruleList = data;
                if (ruleList != null && ruleList.size() > 0) {
                    if (withdrawType != null) {
                        for (WithdrawRule withdrawRule : ruleList) {
                            if (withdrawType.equals(withdrawRule.getDiscountType())) {
                                mRuleList.add(withdrawRule);
                            }
                        }
                    }
                    if (mRuleList.size() > 0) {
                        mAdapter.setWithdrawRules(mRuleList);
                        mAdapter.notifyDataSetChanged();
                    } else {
                        getActivity().finish();
                        ToastUtil.getInstance().showToast(getActivity(), "暂无提现方式");
                    }
                } else {
                    getActivity().finish();
                    ToastUtil.getInstance().showToast(getActivity(), "暂无提现方式");
                }
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                dismissDialog();
            }
        });
    }

    private void initListener() {
        mAdapter.setOnCheckClickListener(new WithdrawWayAdapter.OnCheckClick() {
            @Override
            public void checkBoxClick(int position) {
                if (position > -1) {
                    extractBtn.setEnabled(true);
                    mCurrentRule = mRuleList.get(position);
                    mTextRate.setVisibility(View.VISIBLE);
                    mTextRate.setText("收取"+mCurrentRule.getCounterFee()+"%手续费");
                }
            }
        });
    }

    public void onViewClicked() {
        if (mCurrentRule != null) {
            EventBean<Object> bean = new EventBean<>(EventBean.STATUS_WITHDRAW_RATE);
            bean.setEvent(mCurrentRule);
            EventBus.getDefault().post(bean);
            getActivity().finish();
        } else {
            ToastUtil.getInstance().showToast(getActivity(), "请选择提现方式");
        }
    }
}
